package com.swind.fast.modules.app.halali.partner.service.impl;

import com.swind.fast.common.util.oConvertUtils;

import com.swind.fast.modules.app.halali.partner.entity.RestaurantCategory;
// import com.swind.fast.modules.app.halali.partner.entity.RestaurantCategoryBill;
import com.swind.fast.modules.app.halali.partner.mapper.RestaurantCategoryMapper;
import com.swind.fast.modules.app.halali.partner.service.IRestaurantCategoryService;
// import com.swind.fast.modules.app.halali.partner.model.SpaceAnnouncementModel;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import javax.annotation.Resource;

import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import org.apache.shiro.SecurityUtils;
import com.swind.fast.common.system.vo.HalaliLoginUser;
import com.swind.fast.modules.app.halali.partner.vo.request.RankInfoVo;
import com.swind.fast.modules.app.halali.partner.vo.request.CategoryVo;
import com.swind.fast.modules.app.halali.partner.vo.request.CategoryUpdateVo;
import org.springframework.beans.factory.annotation.Value;


/**
 * @Description: 项目
 * @Author: swind
 * @Date:   2021-11-20
 * @Version: V1.0
 */
@Slf4j
@Service
public class RestaurantCategoryServiceImpl extends ServiceImpl<RestaurantCategoryMapper, RestaurantCategory> implements IRestaurantCategoryService {

    @Value(value = "${swind.path.preview}")
    private String previewPath;

    public RestaurantCategory saveMain(CategoryVo item) {
        HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getRestaurantId();

        RestaurantCategory cat = new RestaurantCategory();
        oConvertUtils.copyPropertiesIgnoreNull(item, cat);
        cat.setRestaurantId(id);
        cat.setCategoryId("-1");
        cat.setGlobal(0);
        cat.setEnabled(1);

        this.save(cat);
        return cat;
    }

    public List<RestaurantCategory> getCategoryList() {
        HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getRestaurantId();
        List<RestaurantCategory> list = this.baseMapper.getCategoryList(id);
        for (RestaurantCategory cat : list) {
            this.renderPic(cat);
        }
        return list;
    }

    public void updateRank(List<RankInfoVo> items) {
        List<RestaurantCategory> list = new ArrayList<RestaurantCategory>();
        for (RankInfoVo item : items) {
            list.add(oConvertUtils.entityToModel(item, RestaurantCategory.class));
        }
        this.updateBatchById(list, 50);
    }

    public RestaurantCategory updateInfo(CategoryUpdateVo item) {
        RestaurantCategory cat = new RestaurantCategory();
        oConvertUtils.copyPropertiesIgnoreNull(item, cat);
        this.updateById(cat);
        return cat;
    }

    private void renderPic(RestaurantCategory info) {
        if (oConvertUtils.isNotEmpty(info.getPic()) && !info.getPic().startsWith("http")) {
            String picPath = this.previewPath + "/" + info.getPic();
            info.setPic(picPath);
        }
    }

}
